#include "mainwindow.h"

#include<QListWidgetItem>
#include<QDebug>
#include<QTcpSocket>
#include<QComboBox>
#include<QMessageBox>
#include<QHostAddress>
#include<QMessageBox>
#include<QVBoxLayout>
#include<QPushButton>
#include<QNetworkProxy>
#include<QProcess>
#include <QListWidget>
#include<QNetworkInterface>
#include<QList>
#include<QMouseEvent>
#include<QCloseEvent>
#include<QToolBar>
#include<QAction>
#include<QRect>
#include<QFont>
#include<QToolButton>
#include<QSpacerItem>
#include<QLineEdit>
#include<QApplication>
#include <QStandardPaths>
#include<QTimer>
#include<QDir>
#include <QWidget>
#include<QFileInfo>
#include<QFile>
#include<QLibrary>
#include<QDialog>
#include <Windows.h>
#include<QThread>
#include <QSizePolicy>
#include<QDebug>
#include"mycodesetwidget.h"
#include"checkcodewidget.h"
#include"mypeoplewidget.h"
#include"mypeopleinfo.h"
#include"changeidwidget.h"
#include "trueapi.h"

#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"


MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{

  //注册传递信息
  qRegisterMetaType<QVector<Target_Info_Table>>("QVector<Target_Info_Table>&");

  qRegisterMetaType<QVector<OneTaregtBullet>>("QVector<OneTaregtBullet>");
  //注册传递信息
  qRegisterMetaType<QVector<int>>("QVector<int>");
  //注册传递信息
  qRegisterMetaType<QVector<double>>("QVector<double>");

  for(int i = 0;i < toolBarNumber;i++)
    {
      act[i] = nullptr;
    }
  api=new TrueApi();

  create();

//  QProcess process;
//  process.start("tasklist",QStringList() << "/FI" << "IMAGENAME eq main.exe");
//  process.waitForFinished();

//  QString output = QString::fromUtf8(process.readAllStandardOutput());

//  if(output.contains("main.exe"))
//    {
//      MYLOG<<"main.exe is running";


//    }else{
//      MYLOG<<"main.exe is not running";
//      //运行主程序
//      if(!runMain()){
//          QMessageBox::information(this,tr("提示"),tr("启动服务器失败"),QMessageBox::Ok);
//          exit(0);
//        }
//    }

  //  myPeopleInfo = new MyPeopleInfo();
  startSocket();


  startThread();

  startTimer();

//  readText();

  initConnect();

  //首次更新电量和状态
  sendData(api->requestState());
  sendData(api->requestBattery());

  myWidget = new MyWidget();

  //  详细信息界面清除按钮
  connect(myWidget, &MyWidget::mySignalBtn, this, &MainWindow::clearonly_clicked);
  //  传递单个靶的电量数据
  connect(outWork,&OutWork::mySignalBattery,myWidget,&MyWidget::passBatteryData);
  //传递子弹数据
  connect(outWork,&OutWork::currentTargetBullet,myWidget,&MyWidget::passHolesData);

  //  传递单个靶的数据
//  connect(sqliteThread,&SQLiteThread::mySignal,myWidget,&MyWidget::passHolesData);
//  connect(sqliteThread,&SQLiteThread::mySignalTargetName,myWidget,&MyWidget::passName);



  //单双击获取靶号
  connect(myListWidget, &MyListWidget::mySignalIndex, myWidget, &MyWidget::getIndex);
  connect(myListWidget, &MyListWidget::mySignalDoubleIndex, myWidget, &MyWidget::getIndex);

    //传递校准后点的信息
  connect(myWidget,&MyWidget::checkXandY,this,&MainWindow::sendCheckXandY);

}

MainWindow::~MainWindow()
{

  if(tcpClient != nullptr)
    {
      delete tcpClient;
    }
  tcpClient = nullptr;

  if(myListWidget != nullptr)
    {
      delete myListWidget;
    }
  myListWidget = nullptr;

  if(toolBar != nullptr)
    {
      delete toolBar;
    }
  toolBar = nullptr;

  if( myWidget != nullptr)
    {
      delete myWidget;
    }
  myWidget = nullptr;


  if(myGradeTableWidget != nullptr)
    {
      delete myGradeTableWidget;
    }
  myGradeTableWidget = nullptr;

  if(myCodeSetWidget != nullptr)
    {
      delete myCodeSetWidget;
    }
  myCodeSetWidget = nullptr;

  if(checkCodeWidget != nullptr)
    {
      delete checkCodeWidget;
    }
  checkCodeWidget = nullptr;

  if(myPeopleWidget != nullptr)
    {
      delete myPeopleWidget;
    }
  myPeopleWidget = nullptr;



  //  if(myPeopleInfo != nullptr)
  //    {
  //      delete myPeopleInfo;
  //    }
  //  myPeopleInfo = nullptr;

  if(changeIdWidget != nullptr)
    {
      delete changeIdWidget;
    }
  changeIdWidget = nullptr;

  for(int i = 0;i < toolBarNumber;i++)
    {
      if(act[i] != nullptr)
        {
          delete act[i];
        }
      act[i] = nullptr;
    }
  delete threadOne;
    threadOne = nullptr;
  delete threadTwo;
    threadTwo = nullptr;
  delete api;
    api = nullptr;
}

void MainWindow::create()
{
  setWindowTitle("标靶显示系统");
  setWindowIcon(QIcon(":/source/app-icon.gif"));
  //最大化
  setWindowState(Qt::WindowMaximized);
  //清除缓存
  setAttribute(Qt::WA_DeleteOnClose,true);

  //隐藏标题框
  setWindowFlags(Qt::FramelessWindowHint);

  myListWidget = new MyListWidget();


  //获取电脑宽度
  nWidth = GetSystemMetrics(SM_CXSCREEN);
  nHeight = GetSystemMetrics(SM_CYSCREEN);

  int toolweight=nWidth/30;

  //工具栏
  toolBar = new QToolBar(this);
  toolBar->setFixedHeight(50);

  //设置工具按钮
  act[0] = new QToolButton(this);
//  act[0]->setSizePolicy(QSizePolicy(QSizePolicy::Expanding));
  act[0]->setFixedWidth(toolweight);
  act[0]->setText("全部清除");
  //提示语
  act[0]->setToolTip("清空全部靶标的子弹");

  act[1] = new QToolButton(this);
  act[1]->setFixedWidth(toolweight);
  act[1]->setText("单个清除");

  act[1]->setToolTip("清空单个靶的子弹");

  act[2] = new QToolButton(this);
  act[2]->setFixedWidth(toolweight);
  act[2]->setText("编号设置");
  act[2]->setToolTip("对靶标地址进行设置");

  act[3] = new QToolButton(this);
  act[3]->setFixedWidth(toolweight);
  act[3]->setText("校准程序");
  act[3]->setToolTip("对靶标进行校准");

  act[4] = new QToolButton(this);
  act[4]->setFixedWidth(toolweight);
  act[4]->setText("人员分组");
  act[4]->setToolTip("对人员进行分组");

  //  act[5] = new QToolButton(this);
  //  act[5]->setFixedWidth(170);
  //  act[5]->setText("人员检录");
  //  act[5]->setToolTip("对人员进行检录");
  //  act[5]->hide();

  act[5] = new QToolButton(this);
  act[5]->setFixedWidth(toolweight);
  act[5]->setText("成绩导出");
  act[5]->setToolTip("查询打靶数据或对查询数据进行导出");

  act[6] = new QToolButton(this);
  act[6]->setFixedWidth(toolweight);
  act[6]->setText("更改系统ID");
  act[6]->setToolTip("更改系统的id");

  act[7] = new QToolButton(this);
  act[7]->setFixedWidth(toolweight);
  act[7]->setText("升降");

  act[8] = new QToolButton(this);
  act[8]->setFixedWidth(toolweight);
  act[8]->setText("退出程序");

  act[9] = new QToolButton(this);
  act[9]->setFixedWidth(200);
   act[9]->setText("(立靶)当前分组 : 1");
   act[9]->setStyleSheet("background-color: #ffc227;");



  //按钮美化
  setStyleSheet("QToolButton,QComboBox{width: 100px;height: 100px;font-size: 15px;font-bold;border: 1px solid green;font-family: 微软雅黑;border-radius: 17px;background-color: #00ffff}""QToolButton:pressed{background:white;}\
                QComboBox QAbstractItemView::item{height:50px;}");

                for(int i = 0;i < toolBarNumber;i++)
  {
    toolBar->addWidget(act[i]);
    toolBar->addSeparator();
    toolBar->addSeparator();
  }


  //布局
  QVBoxLayout *v_layout = new QVBoxLayout(this);

  v_layout->addWidget(toolBar);

  v_layout->addWidget(myListWidget);

  setLayout(v_layout);
}

//出现在父线程中创建一个子线程    原因：在一个主线程中开了两次服务器线程
void MainWindow::startSocket()
{
  //跨线程？//主线程
  m_socket = new QTcpSocket(this);
  try{
    //去掉代理
    m_socket->setProxy(QNetworkProxy::NoProxy);

    //ip和端口号
    m_socket->connectToHost(QHostAddress(ServerListenIp),ServerListenPort);

    if(m_socket->waitForConnected())
      {
        qDebug() << "connect success";
      }else{
        qDebug() << "connect filed";
      }
  }
  catch(std::exception &ex)
  {
    MYLOG<<ex.what();

    return ;
  }
  qDebug()<<m_socket->thread()<<"    socket324";
    //断开重连10s
    m_reconnectTimer.setInterval(ReconnectTime);

    connect(&m_reconnectTimer,&QTimer::timeout,[=](){

        //重连
        reconnect();
      });

    connect(m_socket,&QTcpSocket::connected,this,&MainWindow::onConnected);
    connect(m_socket,&QTcpSocket::disconnected,this,&MainWindow::onDisconnected);
//  connect(m_socket,&QTcpSocket::disconnected,this,&MainWindow::reconnect);
}

void MainWindow::initConnect()
{
  //双击打开详细界面
  connect(myListWidget,&MyListWidget::mySignalDoubleIndex,this,&MainWindow::doubleClicked);
  //  更新所有靶标
//  connect(sqliteThread,&SQLiteThread::mySignal,myListWidget,&MyListWidget::passHolesData);
  //更新所有靶标电量
  connect(outWork,&OutWork::mySignalBattery,myListWidget,&MyListWidget::passBatteryData);
  //更新所有靶标状态
  connect(outWork,&OutWork::mySignalState,myListWidget,&MyListWidget::passStateData);
  //传递子弹数据
  connect(outWork,&OutWork::currentTargetBullet,myListWidget,&MyListWidget::passHolesData);

  //录入信息提示
  //  connect(outWork,SIGNAL(mySignalInfo(const QString&)),this,SLOT(showInfo(const QString&)));
    connect(changeIdWidget, SIGNAL(mySignalId(const QString&)), this, SLOT(changeId(const QString&)));


  //更新所有靶标状态
  //  connect(outWork,SIGNAL(mySignalState(const QList<int>&)),this,SLOT(updateState(const QList<int>&)));


  //进行信号槽关联
  connect(act[0],SIGNAL(clicked()),this,SLOT(clearAll()));
  connect(act[1],SIGNAL(clicked()),this,SLOT(clearonly_clicked()));
  connect(act[2],SIGNAL(clicked()),this,SLOT(codeset_clicked()));
  connect(act[3],SIGNAL(clicked()),this,SLOT(calib_clicked()));
  connect(act[4],SIGNAL(clicked()),this,SLOT(people_clicked()));
  //  connect(act[5],SIGNAL(clicked()),this,SLOT(info_clicked()));
  connect(act[5],SIGNAL(clicked()),this,SLOT(grade_clicked()));
  connect(act[6],SIGNAL(clicked()),this,SLOT(changeid_clicked()));
  connect(act[8],SIGNAL(clicked()),this,SLOT(close_clicked()));
  connect(act[7],SIGNAL(clicked()),this,SLOT(upORdown_clicked()));

}


void MainWindow::startTimer()
{
//  //每隔5s请求一次
//  state_Timer.setInterval(StateTime);

//  //开启定时器
//  state_Timer.start();

//  //监听定时器发送的信号
//  connect(&state_Timer,&QTimer::timeout,[=](){

//      //更新电量
//      sendData(api->requestState());
//    });

//  //  每隔60s请求一次
//  battery_Timer.setInterval(VoltageTime);

//  battery_Timer.start();

//  //监听定时器发送的信号
//  connect(&battery_Timer,&QTimer::timeout,[=](){

//      //请求电量
//      sendData(api->requestBattery());
//    });

}

void MainWindow::startThread()
{

  threadOne = new QThread(this);
  threadTwo = new QThread(this);

  //传socket出现跨线程！
  tcpClient = new TcpClient(m_socket);

  tcpClient->moveToThread(threadOne);
//主线程
  outWork = new OutWork(this);

//  outWork->moveToThread(threadTwo);

//  sqliteThread = new SQLiteThread("conn_1",group_number,this);

//  connect(threadTwo,SIGNAL(started()),outWork,SLOT(doWork()));
  connect(tcpClient,&TcpClient::signalIsforclientInfo,outWork,&OutWork::doWork);
  connect(m_socket, &QTcpSocket::readyRead, tcpClient, &TcpClient::readMessage);


  connect(this,&MainWindow::destroyed,this,[=](){
      threadOne->quit();
      threadOne->wait();
      threadOne->deleteLater();

//      threadTwo->quit();
//      threadTwo->wait();
//      threadTwo->deleteLater();

//      sqliteThread->quit();
//      sqliteThread->wait();
//      sqliteThread->deleteLater();

    });

  threadOne->start();
  threadTwo->start();
//  sqliteThread->start();
}

////运行main程序
//bool MainWindow::runMain()
//{
//  QString  strm = QApplication::applicationDirPath();
//  strm += "/.main.exe";

//  strm.replace("/","\\");
//  try{
//    QProcess *process = new QProcess(this);

//    if (process->startDetached(strm))
//      {
//        qDebug()  <<"main Running...";
//        return true;
//      }
//    else
//      {
//        qDebug()  <<"mian Running Failed";
//        return false;
//      }
//  }
//  catch(std::exception &ex)
//  {
//    MYLOG<<ex.what();
//    qWarning()<<"MainWindow-runMain"<<ex.what();
//    return false;
//  }
//}

////终止main程序
//void MainWindow::stopMain()
//{

//  // 通过进程名字结束进程
//  QProcess::startDetached("taskkill -t  -f /IM " + QString(".main.exe"));
//  QProcess::startDetached("taskkill -t  -f /IM " + QString(".main.exe"));
//  QProcess::startDetached("taskkill -t  -f /IM " + QString(".main.exe"));
//}

//资源释放
void MainWindow::close_clicked()
{
  if(changeIdWidget && changeIdWidget->isVisible())
    changeIdWidget->setVisible(false);
  //  if(myPeopleInfo->isVisible())
  //    myPeopleInfo->setVisible(false);
  if(myPeopleWidget && myPeopleWidget->isVisible())
    myPeopleWidget->setVisible(false);
  if(checkCodeWidget && checkCodeWidget->isVisible())
    checkCodeWidget->setVisible(false);
  if(myCodeSetWidget && myCodeSetWidget->isVisible())
    myCodeSetWidget->setVisible(false);
  if(myGradeTableWidget && myGradeTableWidget->isVisible())
    myGradeTableWidget->setVisible(false);
  if(myWidget && myWidget->isVisible())
    myWidget->setVisible(false);
  if(upDown && upDown->isVisible())
    checkCodeWidget->setVisible(false);

  if(QMessageBox::Ok == QMessageBox::warning(this,tr("警告"),tr("是否退出程序!"),QMessageBox::Ok,QMessageBox::No))
    {
      closeSocket();
//      stopMain();

      this->close();
  }
}

void MainWindow::clearAll()
{
    QByteArray data = api->requestClearAll();

  sendData(data);
}

//单个清除
void MainWindow::clearonly_clicked()
{
  index2 = myListWidget->getIndex();

  if(index2 == -1)
    {
      QMessageBox::information(this,tr("提示"),tr("请选中靶标!"),QMessageBox::Ok);
    }else{
      QByteArray data = api->requestClearOnly(index2);

      sendData(data);
    }
}

void MainWindow::upORdown_clicked()
{
    if(!upDown)
    {
      upDown = new UporDown();

      connect(upDown,SIGNAL(mysignalUporDown(int)),this,SLOT(sendUporDown(int)));
    }
    upDown->show();
}

//编码设置
void MainWindow::codeset_clicked()
{

  if(!myCodeSetWidget){
      myCodeSetWidget = new MyCodeSetWidget();

      //编码设置发送
      connect(myCodeSetWidget,&MyCodeSetWidget::mySignalCodeSetting,this,&MainWindow::sendYuanXin);
      //传递编码设置索引
      connect(myListWidget,SIGNAL(mySignalIndex(int)),myCodeSetWidget,SLOT(getIndex(int)));

    }
  myCodeSetWidget->show();
}

//校准程序
void MainWindow::calib_clicked()
{
  if(!checkCodeWidget){

      checkCodeWidget = new CheckCodeWidget();

      //传递校准程序索引
      connect(myListWidget,SIGNAL(mySignalIndex(int)),checkCodeWidget,SLOT(getCurrentIndex(int)));
      //开始校准
      connect(checkCodeWidget,SIGNAL(mySignalBegin(int)),this,SLOT(sendBegin(int)));
      //结束
      connect(checkCodeWidget,SIGNAL(mySignalOver(int)),this,SLOT(sendOver(int)));
    }
  checkCodeWidget->show();
}

//人员分组
void MainWindow::people_clicked()
{
  if(!myPeopleWidget){

      myPeopleWidget = new MyPeopleWidget();

      //分组编号
      connect(myPeopleWidget, &MyPeopleWidget::mySignalZuHao, this, &MainWindow::sendGroupNumber);
    }
  myPeopleWidget->show();
}

//人员检录
//void MainWindow::info_clicked()
//{
//  if(!myPeopleInfo){

//      myPeopleInfo = new MyPeopleInfo();
//      connect(myPeopleInfo,SIGNAL(mySignalOpen()),this,SLOT(infoOpen()));
//      connect(myPeopleInfo,SIGNAL(mySignalClose()),this,SLOT(infoClose()));
//    }
//  myPeopleInfo->show();
//}

//成绩导出
void MainWindow::grade_clicked()
{
  if(!myGradeTableWidget){

      myGradeTableWidget = new MyGradeTableWidget();
      //获取查询标志
      connect(myGradeTableWidget,&MyGradeTableWidget::signalSearchdate,this,&MainWindow::sendSearchData);
      //传递成绩导出的信息
      connect(outWork,&OutWork::scoreTargetInfo,myGradeTableWidget,&MyGradeTableWidget::updateGrade);
      //传递改变的日期
      connect(myGradeTableWidget,&MyGradeTableWidget::signalDateChange,this,&MainWindow::sendDateChande);
      //传递改变的靶号和组号
      connect(myGradeTableWidget,&MyGradeTableWidget::signal_addr_groupnumber_change,this,&MainWindow::sendAddrAndGroupnumber);

      connect(myGradeTableWidget,&MyGradeTableWidget::signalDateChange,this,&MainWindow::sendDateChande);

    }
  myGradeTableWidget->show();
}

//更改系统id
void MainWindow::changeid_clicked()
{
  if(!changeIdWidget){

      changeIdWidget = new ChangeIdWidget();
    }
  changeIdWidget->show();
}

//详细信息界面
void MainWindow::doubleClicked(int msg)
{
  sendData(api->requestBattery());

  if(!myWidget){
      myWidget = new MyWidget();
    }
  myWidget->show();
}

void MainWindow::sendData(const QByteArray& msg)
{
  try{
    if(m_socket->state() == QAbstractSocket::ConnectedState)
      {
        m_socket->write(msg);
        MYLOG<<msg;

        m_socket->flush();
      }
  }
  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
    qWarning()<<"MainWindow-sendData"<<ex.what();
    return ;
  }
}

void MainWindow::closeSocket()
{
  m_socket->close();
  m_socket->deleteLater();
}

//更改编号控制信息
void MainWindow::sendYuanXin(int old_addr, int new_addr)
{
  QByteArray data = api->sendYuanXin(old_addr,new_addr);

  sendData(data);
}

//开始校准控制信息
void MainWindow::sendBegin(int flag)
{
  QByteArray data = api->sendBegin(flag);

  sendData(data);
}

//结束校准控制信息
void MainWindow::sendOver(int flag)
{
  QByteArray data = api->sendOver(flag);

  sendData(data);
}

//查询数据
void MainWindow::sendSearchData(int fenZu, int baHao, const QString& time)
{
  QByteArray data = api->requestSearchDataApi(fenZu,baHao,time);

  sendData(data);
}

//发送当前改动的日期
void MainWindow::sendDateChande(QString time)
{
  QByteArray data = api->sendDateChange(time);

  sendData(data);
}

void MainWindow::sendAddrAndGroupnumber(int addr, int groupnumber)
{
  QByteArray data = api->sendAddrAndGroupnumber(addr,groupnumber);

  sendData(data);
}

//分组控制信息
void MainWindow::sendGroupNumber(const QString& indexZuHao)
{
  s_group = group_number;

  act[8]->setText("(立靶)当前分组 : " + indexZuHao);

  group_number = indexZuHao.toInt();

  QByteArray data = api->sendGroupNumber(group_number);

  sendData(data);
}

void MainWindow::sendCheckXandY(int addr,int x, int y)
{
  QByteArray data = api->sendXandY(addr,x,y);

  sendData(data);

  //结束校准
  //  sendOver(addr);
}

void MainWindow::sendUporDown(int code)
{
  QByteArray data = api->sendUporDownmsg(code);

  sendData(data);
}


void MainWindow::changeId(const QString &str)
{
  int id = str.toInt();
  MYLOG<<id;
  QByteArray data = api->sendId(id);
  MYLOG<<data;
  sendData(data);
}

//void MainWindow::updateState(const QList<int> &vec)
//{

//  vecState = vec;
//  MYLOG<<vecState;
//}

//void MainWindow::bindInfo(const QString& name,const QString& idCard)
//{
//  for(int i = 1;i < vecState.size();i++)
//    {

//      if(vecState[i] == flag_5)
//        {
//          MYLOG<<"-";
//          QByteArray data = bindInfoApi(i,name,idCard);
//          MYLOG<<data;
//          sendData(data);
//          vecState[i] = flag_3;
//          return;
//        }
//    }
//  MYLOG<<vecState;
//}

//void MainWindow::showInfo(const QString &msg)
//{

//}


void MainWindow::infoOpen()
{
  MYLOG<<"开始信息录入....";
}

void MainWindow::infoClose()
{
  MYLOG<<"结束信息录入....";
}

void MainWindow::onDisconnected()
{
  MYLOG<<"Disconnect from server";
  m_reconnectTimer.start();
}

void MainWindow::onConnected()
{
  MYLOG<<"Connected server";
  m_reconnectTimer.stop();
}

void MainWindow::reconnect()
{
  MYLOG<<"Reconnecting...";

  //ip和端口号
  m_socket->connectToHost(QHostAddress(ServerListenIp),ServerListenPort);
}

void MainWindow::readText()
{
  try{
    m_date = QDate::currentDate();
    currentDate = m_date.toString("yyyy-MM-dd");

    QString  strm = QApplication::applicationDirPath();
    strm += "/assets/config.json";

    QFile file(strm);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
      {
              qDebug() << ("打开文件失败769");
        return ;
      }
    QTextStream txtInput(&file);
    QVector<QString> vecStr;
    QString lineStr;
    while(!txtInput.atEnd())
      {

        lineStr = txtInput.readLine();
        vecStr.push_back(lineStr);
      }

    QString id,str;
    if(vecStr.size() > 0)
      {

        id = vecStr[vecStr.size() - 1];

        if(vecStr.size() > 1)
          {
            //            changeId(id);
          }
        str = vecStr.at(0);
        str.replace("\n","");

        QString s1,s2;
        for(int i = 0;i < 10;i++)
          {
            s1.append(str.at(i));
          }
        for(int i = 11;i < str.size();i++)
          {
            s2.append(str.at(i));
          }

        if(s1 == currentDate)
          {

            group_number = s2.toInt();

            s_group = group_number;
          }
      }
    file.close();
  }

  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
    qWarning()<<"MainWindow-readText"<<ex.what();
    return ;
  }
}

bool MainWindow::deleteFile(const QString &strPath)
{
  try{
    if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
      return false;

    QFileInfo FileInfo(strPath);

    if (FileInfo.isFile())//如果是文件
      QFile::remove(strPath);
    return true;
  }

  catch(std::exception &ex)
  {
    MYLOG<<ex.what();
        qWarning()<<"MainWindow-deleteFile"<<ex.what();
    return false;
  }
}

//bool MainWindow::readCard()
//{
//  try{
//    QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

//    QString strPath = homeDir + "//AppData//Local//Temp//chinaidcard//wz.txt";
//    MYLOG<<"-";
//    if (QDir().exists(strPath))//是否传入了空的路径||路径是否存在
//      {
//        if(!deleteFile(strPath))
//          qDebug()<<"delete file error!";
//      }

//    QLibrary mylib("./Termb.dll");

//    if(mylib.load())
//      {
//        qDebug() << "Termb加载成功";
//      }else{
//        qDebug() << "Termb加载失败";
//      }

//    typedef int (*CVR_InitComm)(int);
//    typedef int (*CVR_Authenticate)(void);
//    typedef int (*CVR_Read_Content)(int);
//    typedef int(* CVR_CloseComm)(void);
//    CVR_InitComm cVR_InitComm = (CVR_InitComm) mylib.resolve("CVR_InitComm");
//    CVR_Authenticate cVR_Authenticate = (CVR_Authenticate) mylib.resolve("CVR_Authenticate");
//    CVR_Read_Content cVR_Read_Content = (CVR_Read_Content) mylib.resolve("CVR_Read_Content");
//    CVR_CloseComm cVR_CloseComm = (CVR_CloseComm) mylib.resolve("CVR_CloseComm");

//    cVR_InitComm(1001);
//    cVR_Authenticate();
//    cVR_Read_Content(4);
//    cVR_CloseComm();

//    if(!cVR_InitComm){
//        //      qWarning() << mylib.errorString();
//        qDebug()<<"mylib读取失败";
//        return false;
//      }

//    QFile f(strPath);

//    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
//      {
//        qDebug() <<"打开文件失败";
//        return false;
//      }
//    QTextStream txtInput(&f);
//    QVector<QString> vecStr;
//    QString lineStr;
//    while(!txtInput.atEnd())
//      {
//        lineStr = txtInput.readLine();
//        vecStr.push_back(lineStr);

//        qDebug() << lineStr;
//      }

//    f.close();

//    if(s_group != group_number)
//      {
//        vecIdCard.clear();
//        s_group = group_number;
//      }

//    int vecSize = vecIdCard.size();

//    QString name = vecStr[0];
//    QString idCard = vecStr[5];

//    vecIdCard.push_back(idCard);

//    for(int i=0;i<vecIdCard.size();i++){
//        for(int j=i+1;j<vecIdCard.size();j++){
//            if(vecIdCard[j]==vecIdCard[i]){
//                vecIdCard.remove(j);
//                j--;
//              }
//          }
//      }
//    MYLOG<<"-";
//    if(vecSize != vecIdCard.size())
//      {
//        bindInfo(name,idCard);
//        MYLOG<<"-";
//        //qDebug()<<"人员信息绑定...";
//      }
//  }

//  catch(std::exception &ex)
//  {
//    MYLOG<<ex.what();
//    qWarning()<<"MainWindow-readCard"<<ex.what();
//    return false;
//  }
//  return true;
//}

//QString MainWindow::getMyIpString()
//{
//  QString ip_address;
//  QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // 获取所有ip
//  for (int i = 0; i < ipAddressesList.size(); ++i)
//    {

//      QHostAddress ipAddr = ipAddressesList.at(i);
//      if ((ipAddr.protocol() == QAbstractSocket::IPv4Protocol))// 筛选出ipv4
//        {

//          if (!ipAddr.toString().startsWith("169"))// 过滤掉网卡，路由器等的ip
//            {
//              ip_address = ipAddr.toString();
//              break;
//            }
//        }
//    }
//  if (ip_address.isEmpty())
//    ip_address = QHostAddress(QHostAddress::LocalHost).toString();
//  return ip_address;
//}





